
import Toast from '../../../component/vant/toast/toast';
import Dialog from '../../../component/vant/dialog/dialog';
var app = getApp(), fetchApi = require("../../../utils/httpPost.js")
const message = '是否确认删除该商品？';
Page({
  data: {
    imgurl: app.getImgUrl(),
    cartList: [],
    goodscount: 0,
    scrollTop: 0,
    windowWidth: 0,
    hasData:false,//是否有数据
    netWork:true,
    accountNum:0,//所有加入购物车的商品数量
    allPrice:0.00,
    visibleModal:false,
    itemData: [],
    checkAll: true,
  },
  tapMove: function (e) {
    this.setData({ scrollTop: this.data.scrollTop + 10 })
  },
  onLoad: function (res) {
    // wx.setNavigationBarTitle({ title: '购物车' })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    that.data.checkAll = true
    that.onHttpInit()
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/product/cart/list', {}, 
    function (res) {
      console.log('购物车列表', res)
      if (res.data.code == 0) {
        console.log('购物车列表 size', res.data.data.length)
        if (res.data.data.length > 0) {
          that.data.cartList = res.data.data
          that.setData({ cartList: res.data.data, hasData: true })
          that.getAllProductCount(res.data.data)
        } else {
          that.setData({  cartList: [], hasData: false  })
        }
        wx.stopPullDownRefresh() //停止下拉刷新
      } else {
        console.log("商品详情数据 异常", res.data.msg)
      }
    },function(){})
  },
  getAllProductCount:function(allcars){
    console.log('allcars',allcars)
    var that = this
    var count = 0;
    var price = 0.00
    that.data.checkAll = true
    for (var index in allcars){
      for (var indexCart in allcars[index].carts){
        if (allcars[index].carts[indexCart].checkState == 1){
          count += allcars[index].carts[indexCart].cartNum
          price += allcars[index].carts[indexCart].totalPrice
        }else{
          that.data.checkAll = false
        }
      }
    }
    console.log('price',price)
    this.data.accountNum = count;
    that.setData({ 
      cartList: allcars, 
      accountNum:count,
      allPrice:price * 100, 
      checkAll: that.data.checkAll
    })
  },
  pushToDetail(e){
    var id = e.currentTarget.dataset.item.product.id;
    wx.navigateTo({
      url: '/pages/product/detail/index?id='+id,
    })
  },
  onChangeCheckAll(val){
    console.log('onChangeCheckAll',val);
    this.setData({
      checkAll: !this.data.checkAll
    })
    var now_state ;
    if(this.data.checkAll){//全选
      console.log('全选')
      now_state = 1;
    }else{//去掉全选
      console.log('去掉全选')
      now_state = 0;
    }
    this.onHttpChangeCartState(undefined,now_state,0,0,1)
  },
  selectTap:function(t){
    console.log('selectTap',t)
    var that = this
    var item_data = t.currentTarget.dataset.item
    var index = t.currentTarget.dataset.index;
    var cartIndex = t.currentTarget.dataset.cartindex;
    var now_state = item_data.checkState == 1 ? 0 : 1;
    this.onHttpChangeCartState(item_data,now_state,index,cartIndex,0)
  },
  onHttpChangeCartState(item_data,now_state,mer_index,cart_index,is_all){
    console.log('mer_index',mer_index)
    console.log('cart_index',cart_index)
    var that = this;
    fetchApi.httpPost('/product/changeCartState', {
      cart_id: item_data!=undefined? item_data.id :0,
      is_all:is_all,
      check_state: now_state
    }, function (res) {
      console.log('修改购物车选中状态', res)
      if (res.data.code == 0) {
        if(is_all == 0){
          if (item_data.checkState == 1) {
            that.data.cartList[mer_index].carts[cart_index].checkState = 0
            that.data.checkAll = false
            that.setData({ checkAll: false })
          } else {
            that.data.cartList[mer_index].carts[cart_index].checkState = 1
          }
        }else{
          for(var index in that.data.cartList){
            for (var indexCart in that.data.cartList[index].carts){
              that.data.cartList[index].carts[indexCart].checkState = now_state
            }
          }
        }
        that.getAllProductCount(that.data.cartList)
        that.setData({
          cartList: that.data.cartList,
        })
      }
    },function(){})
  },
  deleteCar: function (t) {
    this.data.itemData = t;
    console.log("deleteCar",t);
    Dialog.confirm({
      title: '标题',
      message
    });
  },
  confirmDel(val){
    var that = this;
    console.log('confirmDel',val)
    if(val.detail == "confirm"){
      var item_data = that.data.itemData.currentTarget.dataset.item;
      var index = that.data.itemData.currentTarget.dataset.index;
      console.log('item_data== ', item_data)
      that.onHttpDel(item_data.id,index);
    }
  },
  /**
   *  删除http请求操作
   */
  onHttpDel:function(cart_id,index){
    var that = this 
    fetchApi.httpPost('/product/delCart', {
      cart_id: cart_id,
    }, function (res) {
      console.log('删除购物车返回数据', res)
      that.setData({ visibleModal: false });
      if (res.data.code == 0) {
        Toast.success('删除成功');
        that.data.cartList.splice(index, 1)
        that.setData({ 
          cartList: that.data.cartList,
          hasData:that.data.cartList.length > 0 ? true: false
         })
        that.getAllProductCount(that.data.cartList)
      } else {
        Toast.fail(res.data.msg);
      }
    },function(){})
  },
  bindAllSelect:function(){
    var that = this
    console.log('cartList', that.data.cartList)
    var check_state = !that.data.checkAll
    fetchApi.httpPost('/shoppingcar/checkallstate', {
      check_state: check_state,//要改的状态
      now_state: that.data.checkAll?1:0//当前的状态
    }, function (res) {
      if (res.data.code == 0) {
        for (var index in that.data.cartList) {
          if (that.data.checkAll == true) {
            if (that.data.cartList[index].checkState == 1) {
              that.data.cartList[index].checkState = 0
            }
          } else {
            if (that.data.cartList[index].checkState == 0) {
              that.data.cartList[index].checkState = 1
            }
          }
        }
        that.data.checkAll = !that.data.checkAll
        that.setData({ cartList: that.data.cartList, checkAll: that.data.checkAll })
      }
    },function(){})
  },
  skuCountChange(e) {
    console.log('skuCountChange',e)
    var detail = e.detail;
    var that = this
    var index = e.currentTarget.dataset.cartIndex;
    var car = that.data.cartList[index]
    console.log('car',car)
    fetchApi.httpPost('/product/changeCartNum', {
      cart_id: car.id,
      count: detail
    }, function (res) {
      console.log('修改购物车数量返回', res)
      if (res.data.code == 0) {
        that.data.cartList[index].cartNum = detail
        that.data.cartList[index].totalPrice = that.data.cartList[index].attrValue.price * detail
        that.setData({
          cartList: that.data.cartList
        })
        that.getAllProductCount(that.data.cartList)
      }
    },function(){})
  },
  GuangGuang: function () {
    wx.switchTab({  url: '/pages/home/home'  })
  },
  bindPayorder:function(){
    if(this.data.accountNum == 0){
      Toast.fail("请选择要支付商品");
      return;
    }
    wx.navigateTo({ url: '/pages/order/settle/index?goType=gobycar'  })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作  下拉刷新
   */
  onPullDownRefresh: function () {
    this.data.pageIndex = 1
    this.onHttpInit()
  }
})